/**********************************************************************/
#include <avr/io.h>
#include "Cusart.h"

#define DDR_IN 0
#define DDR_OUT 1

#define PORT_SEL PORTB
#define PIN_SEL PINB
#define DDR_SEL DDRB

#define PORT_SDI PORTB
#define PIN_SDI PINB
#define DDR_SDI DDRB

#define PORT_SCK PORTB
#define PIN_SCK PINB
#define DDR_SCK DDRB

#define PORT_SDO PORTB
#define PIN_SDO PINB
#define DDR_SDO DDRB

#define PORT_LED PORTC
#define DDR_LED DDRC

#define PB7 7	//--\
#define PB6 6		// |
#define RFXX_SCK 7// |
#define RFXX_SDO 6// |RF_PORT
#define RFXX_SDI 5// |
#define RFXX_SEL 2// |
#define RFXX_DATA 1// |
#define PB0 0//--/


#define SEL_OUTPUT() DDR_SEL |= (1<<RFXX_SEL)
#define HI_SEL() PORT_SEL|= (1<<RFXX_SEL)
#define LOW_SEL() PORT_SEL&=~(1<<RFXX_SEL)

#define SDI_OUTPUT() DDR_SDI |= (1<<RFXX_SDI)
#define HI_SDI() PORT_SDI|= (1<<RFXX_SDI)
#define LOW_SDI() PORT_SDI&=~(1<<RFXX_SDI)

#define SDO_INPUT() DDR_SDO&= ~(1<<RFXX_SDO)
#define SDO_HI() PIN_SDO&(1<<RFXX_SDO)

#define SCK_OUTPUT() DDR_SCK |= (1<<RFXX_SCK)
#define HI_SCK() PORT_SCK|= (1<<RFXX_SCK)
#define LOW_SCK() PORT_SCK&=~(1<<RFXX_SCK)
/*
#define LED_OUTPUT() DDR_LED |=0x0F
#define LED0_ON() PORT_LED&=~(1<<0)
#define LED0_OFF() PORT_LED|= (1<<0)
#define LED0_TRG() PORT_LED^= (1<<0)

#define LED1_ON() PORT_LED&=~(1<<1)
#define LED1_OFF() PORT_LED|= (1<<1)
#define LED1_TRG() PORT_LED^= (1<<1)

#define LED2_ON() PORT_LED&=~(1<<2)
#define LED2_OFF() PORT_LED|= (1<<2)
#define LED2_TRG() PORT_LED^= (1<<2)

#define LED3_ON() PORT_LED&=~(1<<3)
#define LED3_OFF() PORT_LED|= (1<<3)
#define LED3_TRG() PORT_LED^= (1<<3)
*/
unsigned char RF_RXBUF[22];
void RFXX_PORT_INIT(void)
{
	HI_SEL();
	HI_SDI();
	LOW_SCK();
	SEL_OUTPUT();
	SDI_OUTPUT();
	SDO_INPUT();
	SCK_OUTPUT();
}

unsigned int RFXX_WRT_CMD(unsigned int aCmd)
{
	unsigned char i;
	unsigned int temp;
	
	LOW_SCK();
	LOW_SEL();
	
	for(i=0;i<16;i++)
	{
		temp<<=1;
		if(SDO_HI())
		{
			temp|=0x0001;
		}
		
		LOW_SCK();
		if(aCmd&0x8000)
		{
			HI_SDI();
		}
		else
		{
			LOW_SDI();
		}
		
		HI_SCK();
	
		aCmd<<=1;
	};
	
	LOW_SCK();
	HI_SEL();
	
	return(temp);
	}
unsigned char RF01_RDFIFO(void)
{
	unsigned char i,Result;

	LOW_SCK();
	LOW_SDI();
	LOW_SEL();

	for(i=0;i<16;i++)
	{//skip status bits
		HI_SCK();
		HI_SCK();
		LOW_SCK();
		LOW_SCK();
	}
	Result=0;

	for(i=0;i<8;i++)
	{//read fifo data byte
		Result<<=1;
		if(SDO_HI())
		{
			Result|=1;
		}
	
		HI_SCK();
		HI_SCK();
	
		LOW_SCK();
		LOW_SCK();
	};
		
		HI_SEL();
	return(Result);
}


int main(void)
{

	unsigned int intI,intJ;
	unsigned char i,j,ChkSum;
	
	for(intI=0;intI<10000;intI++)
	for(intJ=0;intJ<123;intJ++);
	
	RFXX_PORT_INIT();
	RFXX_WRT_CMD(0x0000);
	RFXX_WRT_CMD(0x918A);//868BAND,134kHz
	
	RFXX_WRT_CMD(0xA640);//868MHz
	RFXX_WRT_CMD(0xC847);//4.8kbps
	RFXX_WRT_CMD(0xC69B);//AFC setting
	
	RFXX_WRT_CMD(0xC42A);//Clock recovery manual control,Digital filter,DQD=4
	RFXX_WRT_CMD(0xC240);//output 1.66MHz
	RFXX_WRT_CMD(0xC080);
	
	RFXX_WRT_CMD(0xCE88);//use FIFO
	RFXX_WRT_CMD(0xCE8B);
	RFXX_WRT_CMD(0xC081);//OPEN RX
	
//	DDRB|=(1<<RFXX_DATA); /* NOT USED */
	DDRD&=~(1<<2);
	
//	LED_OUTPUT();
	
	i=0;


	USART_Init(12);
	USART_Transmit(0x0A);
	sendString_USART("--SOT--");


	while(1)
	{
		while(!(PIND&(1<<2)))
		{//polling the nIRQ data
			RF_RXBUF[i++]=RF01_RDFIFO();//read FIFO data
			if(i==18)
			{
				i=0;
				
				
				

				RFXX_WRT_CMD(0xCE88); //reset FIFO for next frame recognition
				RFXX_WRT_CMD(0xCE8B);
				ChkSum=0;
				
				USART_Transmit(0x0A);
					sendString_USART("-------");
				for(j=0;j<16;j++)
				{
					
					USART_Transmit(0x0A);
					USART_Transmit(RF_RXBUF[j]);
				
					ChkSum+=RF_RXBUF[j]; //calculate checksum
				}
				
				if(
					( ChkSum==RF_RXBUF[16] ) &&
					( ChkSum!=0)
				  )
				{//frame check
					//PORT_LED=~PORT_LED;//receive indication
				}

			
			}
		}
	}

	return 0;
}
